#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@FileName: bulter-volmer.py
@Version: 1.0
@Date: 2023/7/20 19:48
@Description:
"""

import __local__

__local__.find_root("__root1__.py")

import math as ma
import numpy as np
import matplotlib.pyplot as plt
from find_section import find_section
from brent import brent


def funL(phiL):
    i0 = 0.1
    al_a = 0.5
    al_c = 0.5
    iF = 96485.0
    iR = 8.314472
    iT = 293.15
    phiS = 0.0
    Eeq = -0.26
    phiF = 0.0
    eta = phiS - Eeq - phiL - phiF
    iloc = i0 * (ma.exp(al_a * iF * eta / iR / iT) - ma.exp(-al_c * iF * eta / iR / iT))
    return iloc - 560


def funEta(eta):
    i0 = 0.1
    al_a = 0.5
    al_c = 0.5
    iF = 96485.0
    iR = 8.314472
    iT = 293.15
    iloc = i0 * (ma.exp(al_a * iF * eta / iR / iT) - ma.exp(-al_c * iF * eta / iR / iT))
    return iloc


if __name__ == '__main__':
    ilen = 200
    rg1 = (-0.5, 1.0)
    xlst1 = np.linspace(rg1[0], rg1[1], ilen)
    phiLlst = np.array([funL(x) for x in xlst1])

    rg2 = (-0.5, 0.5)
    xlst2 = np.linspace(rg2[0], rg2[1], ilen)
    etalst = np.array([funEta(x) for x in xlst2])

    plt.style.use('_mpl-gallery')
    fig, (axL, axEta) = plt.subplots(nrows=1, ncols=2, figsize=(5, 4))
    axL.plot(xlst1, phiLlst, linewidth=2.0)
    axL.set_title("phiL")

    axEta.plot(xlst2, etalst, linewidth=2.0)
    axEta.set_title("Eta")

    plt.tight_layout()
    plt.show()

    res = find_section(funL)
    print(f"root section {res}")

    res = brent(funL)
    print(f"root {res}")
